package hu.vaes_dot_hrak.szoftlab4;

import hu.vaes_dot_hrak.szoftlab4.Constant.Way;

import java.util.Random;

public class SpinyAntEater extends FieldElement {
	private boolean active;
	private int maxAntEat;
	private int currentEatedAnts;

	public SpinyAntEater(Field position, int maxAntEat) {
		super(position);
		position.spinyAntEaterStepIn(this);
		this.maxAntEat = maxAntEat;
		this.currentEatedAnts = 0;
	}

	public void step() {

		Constant.Way way = getRandomWay();
		if (position.getNeighboorOnWay(way) != null) {
			boolean canStep = position.getNeighboorOnWay(way)
					.onStepableForSpinyAntEater(way);

			if (canStep) {
				position.spinyAntEaterStepOut(this);
				position.getNeighboorOnWay(way).spinyAntEaterStepIn(this);
			}
		}

		if (currentEatedAnts > maxAntEat)
			this.active = false;
	}

	public Constant.Way getRandomWay() {
		Constant.Way result = Constant.Way.UP;
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(5);
		switch (randomInt) {
		case 0:
			result = Constant.Way.LEFTTOP;
			break;
		case 1:
			result = Constant.Way.RIGHTTOP;
			break;
		case 2:
			result = Constant.Way.LEFTDOWN;
			break;
		case 3:
			result = Constant.Way.RIGHTDOWN;
			break;
		case 4:
			result = Constant.Way.UP;
			break;
		case 5:
			result = Constant.Way.DOWN;
			break;
		}
		return result;
	}

	public void setNewPosition(Field field) {
		this.position = field;
	}

	public void increaseEatedAnts() {
		currentEatedAnts++;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	@Override
	public boolean isOnStepable() {
		return true;
	}

	@Override
	public void onStep(Ant ant) {
		ant.die();
		currentEatedAnts++;
	}

	@Override
	public void onAntKillerToxic() {

	}

	@Override
	public boolean spinyAntEaterTryToEat() {
		return false;
	}

	@Override
	public int stonePushableToThis() {
		return 1;
	}

	@Override
	public boolean isPushable() {
		return false;
	}

	@Override
	public void push(Way way) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushedStone(Constant.Way way) {
	}
}
